package com.dongchedi.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dongchedi.entity.CustomerInfo;
import com.dongchedi.mapper.CustomerInfoMapper;
import com.dongchedi.service.CustomerInfoService;
import com.dongchedi.utils.constant.RedisConstantKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author leibidong
 * @description  用户个人信息实现类
 * @date 2021/6/26
 */
@Service
public class CustomerInfoServiceImpl implements CustomerInfoService {
    @Autowired
    CustomerInfoMapper customerInfoMapper;
    @Resource
    RedisTemplate<String,Object> redisTemplate;
    /*
     * @Author: leibidong
     * @Description: 根据用户id，查询用户个人信息
     * @Date: 2021/6/26 19:49
     * @Param: [customerId]
     * @Return: com.dongchedi.entity.CustomerInfo
     **/
    @Override
    public CustomerInfo queryByCustomerId(Integer customerId) {

        CustomerInfo customerInfo=customerInfoMapper.queryByCustomerId(customerId);
        return customerInfo;
    }
    /*
     * @Author: leibidong
     * @Description: 更新用户签名
     * @Date: 2021/6/26 19:50
     * @Param: [customerId, customerDesc]
     * @Return: java.lang.String
     **/
    @Override
    public String updateDesc(Integer customerId, String customerDesc) {
        Integer i=customerInfoMapper.updateDesc(customerId,customerDesc);
        CustomerInfo customerInfo;
        JSONObject json=new JSONObject();
        if(i!=0){
            customerInfo=customerInfoMapper.queryByCustomerId(customerId);
            if(customerInfo!=null) {
                json.put("code", 1);
                json.put("msg", "成功");
                json.put("data", customerInfo);
            }
            String key = RedisConstantKey.getPersonInfo(customerId);
            redisTemplate.opsForValue().set(key,customerInfo);
            return json.toJSONString();
        }else {
                json.put("code", 0);
                json.put("msg", "更新失败");
                json.put("data", null);
                return json.toJSONString();
        }
    }
    /*
     * @Author: leibidong
     * @Description: 更新用户的头像
     * @Date: 2021/6/26 19:51
     * @Param: [customerId, customerPicUrl]
     * @Return: java.lang.String
     **/
    @Override
    public String updatePic(Integer customerId,String customerPicUrl) {
        Integer i=customerInfoMapper.updatePic(customerId,customerPicUrl);
        JSONObject json=new JSONObject();
        CustomerInfo customerInfo;
        if(i!=0){
            customerInfo=customerInfoMapper.queryByCustomerId(customerId);
            if(customerInfo!=null) {
                json.put("code", 1);
                json.put("msg", "成功");
                json.put("data", customerInfo);
            }
            String key = RedisConstantKey.getPersonInfo(customerId);
            redisTemplate.opsForValue().set(key,customerInfo);
            return json.toJSONString();
        }else {
            json.put("code", 0);
            json.put("msg", "更新失败");
            json.put("data", null);
            return json.toJSONString();
        }
    }
    /*
     * @Author: leibidong
     * @Description: 更新用户昵称
     * @Date: 2021/6/26 19:51
     * @Param: [customerId, customerName]
     * @Return: java.lang.String
     **/
    @Override
    public String updateName(Integer customerId,String customerName) {
        Integer i=customerInfoMapper.updateName(customerId,customerName);
        CustomerInfo customerInfo;
        JSONObject json=new JSONObject();
        if(i!=null){
            customerInfo=customerInfoMapper.queryByCustomerId(customerId);
            if(customerInfo!=null) {
                json.put("code", 1);
                json.put("msg", "成功");
                json.put("data", customerInfo);
            }
            String key = RedisConstantKey.getPersonInfo(customerId);
            redisTemplate.opsForValue().set(key,customerInfo);
            return json.toJSONString();
        }else {
            json.put("code", 0);
            json.put("msg", "更新失败");
            json.put("data", null);
            return json.toJSONString();
        }
    }
}
